package com.info.zhiduoduo.pay.sdk.dayspaysdk.core.client.http;

import com.info.zhiduoduo.pay.sdk.dayspaysdk.core.util.Strings;

import org.apache.log4j.Logger;

import java.io.BufferedInputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

/**
 * http���շ����ߣ�UTF-8����
 * @author xie
 *
 */
@Slf4j
public class Httper {

	private static Logger logger = Logger.getLogger(Httper.class.getName());

	/**
	 * ��ȡhttp���ӣ�֧���Զ����ݣ����ӳ�ʱ����£�
	 * @param urls 
	 * @param connTimeOut 
	 * @param readTimeOut 
	 * @return HttpURLConnection
	 */
	public static HttpURLConnection getHttpURLConnection(String[] urls,int connTimeOut,int readTimeOut,String content) {

		//��ȡ���ӣ�ֻҪexception�������Ի�ȡ��һ��
		HttpURLConnection connect = null;
		for(int i = 0;i < urls.length;i++) {

			try {

				String url = urls[i];

				logger.info("connect to "+url);
				url=url+"?request_text="+content;
				connect = new SslConnection().openConnection(url);
				connect.setConnectTimeout(connTimeOut*1000);
				connect.setReadTimeout(readTimeOut*1000);

				connect.setRequestMethod("POST");
				connect.setDoInput(true);
				connect.setDoOutput(true);
				connect.connect();


				return connect;

			} catch (Exception e) {
				logger.error(Strings.toString(e));
				if(connect != null) {
					try {
						connect.disconnect();
					} catch(Exception ex){
						logger.error(Strings.toString(ex));
					}
				}
			}
		}

		return null;
	}
	
	/**
	 * ��ȡhttp���ӣ�֧���Զ����ݣ����ӳ�ʱ����£�
	 * @param urls 
	 * @param connTimeOut 
	 * @param readTimeOut 
	 * @return HttpURLConnection
	 */
	public static HttpURLConnection getHttpURLConnection(String[] urls,int connTimeOut,int readTimeOut) {

		//��ȡ���ӣ�ֻҪexception�������Ի�ȡ��һ��
		HttpURLConnection connect = null;
		for(int i = 0;i < urls.length;i++) {

			try {

				String url = urls[i];

				//logger.info("connect to "+url);
				//url=url+"?request_text="+content;
				connect = new SslConnection().openConnection(url);
				connect.setConnectTimeout(connTimeOut*1000);
				connect.setReadTimeout(readTimeOut*1000);

				connect.setRequestMethod("POST");
				connect.setDoInput(true);
				connect.setDoOutput(true);
				connect.connect();


				return connect;

			} catch (Exception e) {
				//logger.error(Strings.toString(e));
				if(connect != null) {
					try {
						connect.disconnect();
					} catch(Exception ex){
						logger.error(Strings.toString(ex));
					}
				}
			}
		}

		return null;
	}

	/**
	 * ���Ͳ�ͬ���������ݣ������޳���
	 * @param urls
	 * @param connTimeOut
	 * @param readTimeOut
	 * @param content
	 * @return
	 */
	public static String sendAndGet(String[] urls,int connTimeOut,int readTimeOut,String content) {

		HttpURLConnection connect = null;

		try {
			//urls=urls+"?request_text="+content;
			connect = getHttpURLConnection(urls,connTimeOut,readTimeOut);

			byte[] put = content.getBytes("UTF-8");
			

			OutputStream out = connect.getOutputStream();
			out.write(put);
			out.flush();
			out.close();

			int responseCode = connect.getResponseCode();
			log.info("responseCode=" + responseCode);
			return new String(StreamReader.readHttp(connect.getInputStream()),"UTF-8");

		} catch(Exception e) {
			log.error(Strings.toString(e));
		} finally {
			if(connect != null) {
				try {
					connect.disconnect();
				} catch(Exception e){
					log.error(Strings.toString(e));
				}
			}
		}

		return null;
	}

	/**
	 * ���Ͳ�ͬ���������ݣ������г���λ
	 * @param urls
	 * @param connTimeOut
	 * @param readTimeOut
	 * @param content
	 * @param msgLen
	 * @return
	 */
	public static String sendAndGet(String[] urls,int connTimeOut,int readTimeOut,String content,int msgLen) {

		HttpURLConnection connect = null;

		try {
			connect = getHttpURLConnection(urls,connTimeOut,readTimeOut);

			byte[] put = (Strings.padLeft(content.getBytes("UTF-8").length+"",'0', msgLen) + content).getBytes("UTF-8");

			OutputStream out = connect.getOutputStream();
			out.write(put);
			out.flush();
			out.close();

			BufferedInputStream in = new BufferedInputStream(connect.getInputStream());
			byte[] lb = new byte[msgLen];
			int ll = 0;
			int lr = 0;
			while(ll < msgLen && (lr = in.read(lb,ll,msgLen-ll)) != -1) {
				ll += lr;      
			}
			if(ll < msgLen) {
				logger.warn("readLen invalid length:" + ll);	               
			}
			int tl = Integer.parseInt(new String(lb));
			logger.info("readLen:" + tl);	            
			byte[] bts = new byte[tl];
			int totalLen = 0;
			int len = 0;
			while (totalLen < tl && (len = in.read(bts, totalLen, tl-totalLen)) != -1) {
				totalLen += len;
				logger.info("readLen:" + tl);
			}
			if(totalLen < tl) {
				logger.info("invalid request,read:" + totalLen + ",total:" + tl);
			}

			return new String(bts, "UTF-8");

		} catch(Exception e) {
			logger.error(Strings.toString(e));
		} finally {
			if(connect != null) {
				try {
					connect.disconnect();
				} catch(Exception e){
					logger.error(Strings.toString(e));
				}
			}
		}

		return "";
	}

	/**
	 * ��ȡ������������
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static String read(HttpServletRequest request) throws Exception {

		return new String(StreamReader.readHttp(request.getInputStream()),"UTF-8");
	}
}
